package util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.Jsoup;

class InsertJob implements Runnable {
	String data = "";

	public InsertJob(String data) {
		this.data = data;
	}

	public void run() {
		try {
			String action = "put";
			String url = S3QueueHandle.queueServer + "?name="
					+ S3QueueHandle.queueName + "&opt=" + action + "&data="
					+ data;
			Jsoup.connect(url).timeout(10000).referrer(url).execute();
		} catch (Exception e) {
			System.err.println(e.getMessage() + " - " + this.data);
		}
	}

}

public class S3QueuePut {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		int limit = 1000000;
		int offset = 720000;
		try {
			ExecutorService executorService = Executors.newFixedThreadPool(150);
			BufferedReader reader = new BufferedReader(new FileReader(
					"top-1m.csv"));
			String line = null;
			int i = 0;
			while ((line = reader.readLine()) != null) {

				if (i >= offset & i <= limit) {
					String row = line;
					executorService.execute(new InsertJob(row));
				}
				i++;
			}
			System.out.println("Finish");
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}
}
